home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ViewportLayout.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  179 lines

  1. /*
  2.  * @(#)ViewportLayout.java    1.16 98/03/16
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.LayoutManager;
  24. import java.awt.Component;
  25. import java.awt.Container;
  26. import java.awt.Rectangle;
  27. import java.awt.Point;
  28. import java.awt.Dimension;
  29. import java.awt.Insets;
  30. import java.io.Serializable;
  31.  
  32. /**
  33.  * The default layout manager for JViewport.  JViewportLayout defines 
  34.  * a policy for layout that should be useful for most applications.
  35.  * The viewport makes its view the same size as the viewport, 
  36.  * however it will not make the view smaller than its minimum size.
  37.  * As the viewport grows the view is kept bottom justified until 
  38.  * the entire view is visible, subsequently the view is kept top
  39.  * justified.
  40.  * <p>
  41.  * Warning: serialized objects of this class will not be compatible with
  42.  * future swing releases.  The current serialization support is appropriate 
  43.  * for short term storage or RMI between Swing1.0 applications.  It will
  44.  * not be possible to load serialized Swing1.0 objects with future releases
  45.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  46.  * baseline for the serialized form of Swing objects.
  47.  *
  48.  * @version 1.16 03/16/98
  49.  * @author unknown
  50.  */
  51. public class ViewportLayout implements LayoutManager, Serializable
  52. {
  53.     /**
  54.      * Adds the specified component to the layout. Not used by this class.
  55.      * @param name the name of the component
  56.      * @param comp the the component to be added
  57.      */
  58.     public void addLayoutComponent(String name, Component c) { }
  59.  
  60.     /**
  61.      * Removes the specified component from the layout. Not used by
  62.      * this class.  
  63.      * @param comp the component to remove
  64.      */
  65.     public void removeLayoutComponent(Component c) { }
  66.  
  67.  
  68.     /**
  69.      * Returns the preferred dimensions for this layout given the components
  70.      * in the specified target container.
  71.      * @param target the component which needs to be laid out
  72.      * @return a Dimension object containing the preferred dimensions
  73.      * @see target the Container to be laid out
  74.      * @see #minimumLayoutSize
  75.      */
  76.     public Dimension preferredLayoutSize(Container parent) {
  77.     Component view = ((JViewport)parent).getView();
  78.     if (view == null) {
  79.         return new Dimension(0, 0);
  80.     }
  81.     else if (view instanceof Scrollable) {
  82.         return ((Scrollable)view).getPreferredScrollableViewportSize();
  83.     } 
  84.     else {
  85.         return view.getPreferredSize();
  86.     } 
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Returns the minimum dimensions needed to layout the components
  92.      * contained in the specified target container.
  93.      *
  94.      * @param target the component which needs to be laid out
  95.      * @return a Dimension object containing the minimum dimensions
  96.      * @see #preferredLayoutSize
  97.      */
  98.     public Dimension minimumLayoutSize(Container parent) {
  99.     return new Dimension(4, 4);
  100.     }
  101.  
  102.  
  103.     /**
  104.      * Called by the AWT when the specified container needs to be laid out.
  105.      *
  106.      * @param parent  the container to lay out
  107.      *
  108.      * @exception AWTError  if the target isn't the container specified to the
  109.      *                      BoxLayout constructor
  110.      */
  111.     public void layoutContainer(Container parent) 
  112.     {
  113.     JViewport vp = (JViewport)parent;
  114.     Component view = vp.getView();
  115.     Scrollable scrollableView = null;
  116.  
  117.     if (view == null) {
  118.         return;
  119.     } 
  120.     else if (view instanceof Scrollable) {
  121.         scrollableView = (Scrollable) view;
  122.     }
  123.  
  124.     /* All of the dimensions below are in view coordinates, except
  125.      * vpSize which we're converting.
  126.      */
  127.  
  128.     Insets insets = vp.getInsets();
  129.     Dimension viewPrefSize = view.getPreferredSize();
  130.     Dimension vpSize = vp.getSize();
  131.     Dimension extentSize = vp.toViewCoordinates(vpSize);
  132.     Dimension viewSize = viewPrefSize; 
  133.  
  134.     if (scrollableView != null) {
  135.         if (scrollableView.getScrollableTracksViewportWidth()) {
  136.         viewSize.width = vpSize.width;
  137.         }
  138.         if (scrollableView.getScrollableTracksViewportHeight()) {
  139.         viewSize.height = vpSize.height;
  140.         }
  141.     }
  142.  
  143.     Point viewPosition = vp.getViewPosition();
  144.  
  145.     /* If the new viewport size would leave empty space below the
  146.      * view, top justify the view.
  147.      */
  148.  
  149.     if ((viewPosition.y + extentSize.height) > viewSize.height) {
  150.         viewPosition.y = 0;
  151.     }
  152.     
  153.     /* If the new viewport size would leave empty space to the
  154.      * right of the view, left justify the view.
  155.      */
  156.  
  157.     if ((viewPosition.x + extentSize.width) > viewSize.width) {
  158.         viewPosition.x = 0;
  159.     }
  160.  
  161.     /* If the orgin of the view is showing and the viewport is 
  162.      * bigger than the views preferred size, then make the view
  163.      * the same size as the viewport.  
  164.      */
  165.     
  166.     if ((viewPosition.y == 0) && (vpSize.height > viewPrefSize.height)) {
  167.         viewPrefSize.height = vpSize.height;
  168.     }
  169.     
  170.     if ((viewPosition.x == 0) && (vpSize.width > viewPrefSize.width)) {
  171.         viewPrefSize.width = vpSize.width;
  172.     }
  173.  
  174.     vp.setViewPosition(viewPosition);
  175.     vp.setViewSize(viewPrefSize);
  176.     }
  177. }
  178.  
  179.